﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

namespace Model
{
    public class DeviceModel : BaseModel
    {
        #region Const

        #region Table Plans

        public const string AUTO_ID_PLAN = "AutoId";
        public const string PLAN_NAME = "PlanName";
        public const string PLAN_VCPU = "vCPU";
        public const string PLAN_RAM = "RAM";
        public const string PLAN_DIST_QUOTA = "DistQuota";
        public const string PLAN_BANDWIDTH = "BandWidth";
        public const string PLAN_MONTHLYFEE = "MonthlyFee";
        public const string PLAN_SPECIFICATION_ID = "SpecificationId";

        #endregion

        #region Table OS

        public const string AUTO_ID_OS = "AutoId";
        public const string NAME_OS = "Name";

        #endregion

        #region Table Memory

        public const string MEMORY_ID = "MemoryId";
        public const string MEMORY = "Memory";
        public const string MEMORY_PRICE = "Price";
        public const string MEMORY_SPECIFICATION_ID = "SpecificationId";
        public const string MEMORY_SPECIFICATION = "Specification";

        #endregion

        #region Table CPU

        public const string CPU_ID = "CPUId";
        public const string CPU_PRICE = "Price";
        public const string CPU_vCPU = "vCPU";
        public const string CPU_SPECIFICATION_ID = "SpecificationId";
        public const string CPU_SPECIFICATION = "Specification";

        #endregion

        #region Table DiskSpace

        public const string DISKSPACE_ID = "DistSpaceId";
        public const string DISKSPACE = "DistSpace";
        public const string DISKSPACE_PRICE = "Price";
        public const string DISKSPACE_SPECIFICATION_ID = "SpecificationId";
        public const string DISKSPACE_SPECIFICATION = "Specification";

        #endregion

        #region Table Specification

        public const string SPECIFICATION_TABLE_NAME = "Specification";
        public const string SPECIFICATION_ID = "SpecificationId";
        public const string SPECIFICATION_NAME = "Specification";

        #endregion

        #endregion

        #region Public Methods

        public DataTable GetListPlansTemplate()
        {
            return GetDataTable("GetPlans");
        }

        public DataTable GetListOS()
        {
            return GetDataTable("GetListWindowsType");
        }

        public DataTable GetListCPU()
        {
            return GetDataTable("GetListCPU");
        }

        public DataTable GetListMemory()
        {
            return GetDataTable("GetListMemory");
        }

        public DataTable GetListDiskSpace()
        {
            return GetDataTable("GetListDistSpace");
        }

        public DataTable GetListSpecification()
        {
            return GetDataTable("GetListSpecification");
        }

        public DataRow UpdateDeviceFriendlyNameByDeviceId(int DeviceId, string FriendlyName, string Notes)
        {
            return GetDataRow("UpdateDeviceFriendlyNameByDeviceId", DeviceId, FriendlyName, Notes);
        }

        public DataTable GetDevicesDependFirewallByAccountIdAndFirewallId(int AccountId, int FirewallId)
        {
            return GetDataTable("GetDevicesDependFirewallByAccountIdAndFirewallId", AccountId, FirewallId);
        }        

        public DataTable GetRequestStatus()
        {
            return GetDataTableBySQL("SELECT [RequestStatusId],[RequestStatusName] ,[IsDefault] FROM [dbo].[RequestStatus] WHERE [IsActive]=1 ORDER BY [OrderId]");
        }

        public DataTable GetFirewallsByCustomerId ( int customerId )
        {
            return GetDataTable("GetFirewallsByAccountId", customerId);
        }

        public DataRow GetDevice(int id)
        {
            DataTable table = GetDataTable("GetDevice", id);

            if ( table.Rows.Count > 0 )
            {
                return table.Rows[0];
            }
            return null;
        }

        public DataRow GetFirewall ( int id )
        {
            DataTable table = GetDataTable("GetFirewall", id);

            if ( table.Rows.Count > 0 )
            {
                return table.Rows[0];
            }
            return null;
        }

        public DataTable GetFirewallByFirewallId(int id)
        {
            DataTable table = GetDataTableBySQL("select * from Firewalls where AutoId= "+ id);

            if (table.Rows.Count > 0)
            {
                return table;
            }
            return null;
        } 
        public DataTable GetDevicesByCustomerId ( int customerId )
        {
            return GetDataTable("GetDevicesByAccountId", customerId);
        }
        public DataTable GetListRequestByRequestId(int RequestDeviceId)
        {
            return GetDataTable("GetListRequestByRequestId", RequestDeviceId);
        }

        public DataTable GetDevicesByCustomerIdAndFirewallId ( int customerId, int firewallId )
        {
            return GetDataTable("GetDevicesByFirewallId", customerId, firewallId);
        }

        public DataTable GetDevicesByAccountId(int customerId)
        {
            return GetDataTable("GetDevicesByAccountId", customerId);
        }

        public DataTable GetListDeviceByAccountId ( int AccountId )
        {
            return GetDataTable("GetListDeviceByAccountId", AccountId);
        }
        public DataTable GetListDeviceByAccountIdAndDeviceId(int AccountId, int DeviceId)
        {
            return GetDataTable("GetListDeviceByAccountIdAndDeviceId", AccountId, DeviceId);
        }
        public DataTable GetListDeviceByAccountIdAndFirewallId(int AccountId, int FirewallId)
        {
            return GetDataTable("GetListDeviceByAccountIdAndFirewallId", AccountId, FirewallId);
        }
        public DataTable AddRequestCustomer(int UserId, int vCPU, int Memory, int DiskSpace, int OSId,int PlanId, bool IsCustom)
        {
            return GetDataTable("InsertUpdateRequestDevice", UserId, vCPU, Memory, DiskSpace, OSId, PlanId, IsCustom, 0);
        }

        public int SaveDevice ( int customerId, int id, string name, string cpu, string ram, string diskQuota, string bandwidth, int osId, string ip, double fee, DateTime effectiveFromDate, DateTime effectiveToDate )
        {
            return ( int ) ExecuteScalar("InsertUpdateDevice", customerId, id, name, cpu, ram, diskQuota, bandwidth, osId, ip, fee, effectiveFromDate, effectiveToDate);
        }

        public int SaveFirewall ( int customerId, int id, string code, string name, string ip, double fee, DateTime fromDate, DateTime toDate )
        {
            return ( int ) ExecuteScalar("InsertUpdateFirewall", customerId, id, code, name, ip, fee, fromDate, toDate);
        }
        public DataTable GetListRequestDevice()
        {
            return GetDataTable("GetListRequestDeviceAll");
        }

        public DataTable GetListRequestDeviceByAll(string AccountName, string UserName, DateTime? RequestFromDate, DateTime? RequestToDate, int WindowId, int RequestStatusId)
        {
            return GetDataTable("GetListRequestDeviceByAll", AccountName, UserName, RequestFromDate, RequestToDate, WindowId, RequestStatusId);
        }

        public void DeleteDevice ( int id )
        {
            ExecuteNonDataQuery("DeleteDevice", id);
        }

        public void DeleteFirewall ( int id )
        {
            ExecuteNonDataQuery("DeleteFirewall", id);
        }

        public void DeleteRequestDevice ( int RequestDeviceId )
        {
            ExecuteNonDataQuery("DeleteRequestDevice", RequestDeviceId);
        }
        public void UpdateRequestDevice(int RequestDeviceId, int CPUId, int RAMId, int DistSpaceId, int OSId, string Bandwidth)
        {
            ExecuteNonDataQuery("UpdateRequestDevice", RequestDeviceId, CPUId, RAMId, DistSpaceId, OSId, Bandwidth);
        }
        public void UpdateRequestDeviceStatus(int RequestDeviceId, int RequestStatusId)
        {
            ExecuteNonDataQuery("UpdateRequestDeviceStatus", RequestDeviceId, RequestStatusId);
        }

        public void BindDevicesToFirewall ( int firewallId, string deviceIds )
        {
            ExecuteNonDataQuery("BindDevicesToFirewall", firewallId, deviceIds, ",");
        }

        public void RemoveDevicesFromFirewall ( int firewallId, string deviceIds)
        {
            ExecuteNonDataQuery("RemoveDevicesFromFirewall", firewallId, deviceIds, ",");
        }

        #endregion
    }
}
